<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class member extends CI_Controller {

	var $data;

	public function __construct(){
		parent::__construct();

		$this->load->model('modelmember');

		if(!$this->modelmember->validate_cookie()){
			$this->session->set_flashdata('error', 'Anda tidak mempunyai akses ke halaman ini');
			header('location: '.site_url().'/dashboard/member');
		}else{
			$this->data['cookie'] = $this->modelmember->get_cookie();
		}
	}

	public function edit_profile(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');
		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Dashboard';
		$this->data['tiga'] = 'Edit Profile';	
		$this->data['empat'] = 'profile/edit-profile';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function proses_ubah_profile(){
		$data=array(

			'username'=>$_POST['usr'],
			'password'=>$_POST['pwd'],
			'fullname'=>$_POST['fullname'],
			'email'=>$_POST['email'],
			'lastlog'=>$_POST['lastlog'],
			'telephone'=>$_POST['telephone'],
			'idmember'=>$_POST['idmember'],

		);
		
		$this->load->model('modelmember');
		$res=$this->modelmember->ubah_member($data);
		//echo json_encode($res);
		if($res){
			redirect(site_url());
		}else{
			echo "Pengubahan Data Gagal <br>";
			echo '<a href="'.site_url('member/edit-profile').'">Kembali</a>';
		}
	}
	
	public function dashboard(){
		
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');
		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Dashboard';
		$this->data['tiga'] = 'Halaman Dashboard';	
		$this->data['empat'] = 'dashboard/dashboard';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function produk(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');
		$this->data['kategori'] = $this->modelmember->getKategoriProduk();
		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Produk';
		$this->data['tiga'] = 'CRUD Produk';
		$this->data['empat'] = 'produk/tambah-produk';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function upload(){
       if(!empty($_FILES['file']['name'])){
        $config['upload_path'] = './img';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
        if($this->upload->do_upload('file'))
	        {
	            $image_data = $this->upload->data(); 
	            
	            // database
	            $this->db->insert('tbl_imagemanager',array(
	                'foto'=>$image_data['file_name']
	            )); 
	            
	            $json = array(
	                'filelink' => base_url("img/{$image_data['file_name']}")
	            );
	            echo stripslashes(json_encode($json));
	        }
    	}
	}
    
	public function galeri(){
	    $return=array();
	    $all_data = $this->db->get('tbl_imagemanager');
	    foreach($all_data->result_array() as $row)
	    {
	        $return[]=array(
	            'thumb'=>base_url('img/'.$row['foto']),
	            'image'=>base_url('img/'.$row['foto']),
	            'title'=>$row['foto'],
	        );
	    }
	    echo json_encode($return);
	}

	//halaman kategori controller
	public function kategori(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');

		$this->load->model('modelmember');
        $this->data['hasil'] = $this->modelmember->bacadata();
		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Kategori';
		$this->data['tiga'] = 'CRUD Halaman Kategori';
		$this->data['empat'] = 'kategori/kategori';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	//fungsi tambah kategori controller
	public function proses_tambah_kategori(){
		$data=array(
		'nama_kategori'=>$_POST['nama_kategori'],
		'brand'=>$_POST['brand'],
		);
		$this->load->model('modelmember');
		$res=$this->modelmember->tambah_kategori($data);
		$this->session->set_flashdata('info', "Kategori berhasil di tambahkan.");
		if($res){
			redirect(site_url('member/kategori'));
		}else{
			echo "Penambahan Data Gagal <br>";
			echo '<a href="'.site_url('member/kategori').'">Kembali</a>';
		}
	}

	public function edit_kategori(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');

		$this->load->model('modelmember');
        $this->data['hasil'] = $this->modelmember->bacadata();
		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Kategori';
		$this->data['tiga'] = 'CRUD Halaman Kategori';
		$this->data['empat'] = 'kategori/kategori';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');	
	}

	public function proses_edit_kategori(){
		$data=array(

			'id_kategori'=>$_POST['id_kategori'],
			'nama_kategori'=>$_POST['nama_kategori'],
			'brand'=>$_POST['brand'],
		);
		
		$this->load->model('modelmember');
		$res=$this->modelmember->ubah_kategori($data);
		//echo json_encode($res);
		if($res){
			redirect(site_url('member/edit-profile'));
		}else{
			echo "Pengubahan Data Gagal <br>";
			echo '<a href="'.site_url('member/edit-profile').'">Kembali</a>';
		}
	}
	
	//fungsi Edit kategori controller
	//public function edit_kategori($id_kategori){
	//	$hapus = $this->modelmember->edit_kategori($id_kategori);
	//	redirect('member/kategori');
	//}

	//fungsi Hapus kategori controller
	//public function hapus_kategori($id_kategori){
	//	$hapus = $this->modelmember->hapus_kategori($id_kategori);
	//	$this->session->set_flashdata('info', "Kategori berhasil dihapus.");
	//	redirect('member/kategori');
		
	//}
	
//Banner

	public function banner(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');

		$this->load->model('modelmember');
		$this->data['hasil'] = $this->modelmember->get_data_banner();
		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Tambah Banner';
		$this->data['tiga'] = 'Tambah Banner';
		$this->data['empat'] = 'banner/banner';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function tambah_banner(){
           $config['upload_path'] = 'upload/banner/';
           $config['allowed_types'] = 'gif|jpeg|png|jpg';
           $config['max_size'] = 2000;
           $config['max_height'] = 2000;
           $config['max_width'] = 2000;
           $config['encrypt_name'] = TRUE;
           $this->upload->initialize($config);
           
            if ($this->upload->do_upload('gambar_banner')) {
                $dok = $this->upload->data();
                
            $foto = $dok['file_name'];
            $this->modelmember->tambah_banner($foto);
            
            $this->session->set_flashdata('info', "Banner berhasil ditambahkan.");
            redirect('member/banner');
            }else{
            	$this->session->set_flashdata('error', "Maaf data tidak di pilih.");
                $this->load->model('modelmember');
				$this->data['hasil'] = $this->modelmember->get_data_banner();
				$this->data['user']= $this->session->userdata('c_usr');
				$this->data['satu']  = 'member';
				$this->data['dua'] = 'Tambah Banner';
				$this->data['tiga'] = 'Tambah Banner';
				$this->data['empat'] = 'banner/banner';
				$this->load->view('layout/head');
				$this->load->view('member/header', $this->data);
				$this->load->view('member/sidebar', $this->data);
				$this->load->view('layout/footer');
            }
    }

	public function pesanan(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');

		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Pesanan';
		$this->data['tiga'] = 'CRUD Halaman Pesanan';
		$this->data['empat'] = 'produk/tambah-produk';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function member(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');

		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Member';
		$this->data['tiga'] = 'CRUD Halaman Member';
		$this->data['empat'] = 'member/member';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function laporan(){
		$this->load->model('modelmember');
		$this->data['user']= $this->session->userdata('c_usr');

		$this->data['satu']  = 'member';
		$this->data['dua'] = 'Laporan';
		$this->data['tiga'] = 'Monitoring Laporan';
		$this->data['empat'] = 'laporan/laporan';
		$this->load->view('layout/head');
		$this->load->view('member/header', $this->data);
		$this->load->view('member/sidebar', $this->data);
		$this->load->view('layout/footer');
	}

	public function logout(){
		$this->modelmember->do_logout();
		header('location: '.site_url().'/dashboard/member');
	}
}